import ast

import numpy as np
import pandas as pd
import os
import sys

FILE_ABS_PATH = os.path.dirname(__file__)
ROOT_PATH = os.path.join(FILE_ABS_PATH, os.pardir)
sys.path.append(ROOT_PATH)
from script_convert_rating import filter_unused_data

import warnings

warnings.filterwarnings("ignore")
from multiprocessing.pool import ThreadPool


def process(object):
    line, ID, total_num = object
    if ID % 10000 == 0:
        print(f"ID {ID} / {total_num}")
    if line == '':
        return None
    jins = ast.literal_eval(line)
    rating = [jins['user_id'], jins['business_id'], jins['stars']]
    return rating


def convert_ratings(input_fname, output_fname):
    rating = pd.DataFrame([], columns=['userID', 'itemID', 'rating'])
    with open(input_fname, 'r') as f:
        text = f.read()
        line_l = text.split("\n")

        pool = ThreadPool()
        # line_l = line_l[:1000]
        # line_l.append("")
        data_l = zip(line_l, np.arange(len(line_l)), np.ones(len(line_l), dtype=np.int32) * len(line_l))
        result_l = pool.map(lambda part: process(part), data_l)
        print(result_l)
        result_l = [i for i in result_l if i is not None]
        rating = pd.DataFrame(result_l, columns=['userID', 'itemID', 'rating'])
        print(rating)

    order_l = ['userID', 'itemID', 'rating']
    rating = rating[order_l]
    rating = filter_unused_data.convert_ratings(rating)
    filter_unused_data.stats_rating(rating)

    rating.to_csv(output_fname, index=False)
    print("complete {}".format(output_fname))


if __name__ == '__main__':
    input_fname = os.path.join('/home/bianzheng/Dataset/Recommendation/yelp/yelp_academic_dataset_review.json')
    output_fname = os.path.join('/home/bianzheng/rec2-mips/intermediate-rating-csv/yelp.csv')
    convert_ratings(input_fname, output_fname)
